//
// Created by Arnold on 2021/5/17.
//

#ifndef ZKEP_ATP_THP_COMM_H
#define ZKEP_ATP_THP_COMM_H

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <time.h>


#define RET_SUCCESS								0
#define RET_FAILURE								-1
#define EP_NULL								    NULL


#define EP_FALSE								0
#define EP_TRUE									1

typedef int										EP_INT;
typedef char									EP_CHAR;
typedef int										EP_BOOL;
typedef unsigned int							EP_UINT;
typedef double									EP_DOUBLE;
typedef float									EP_FLOAT;
typedef long									EP_LONG;
typedef long long								EP_LLONG;
typedef unsigned  long long						EP_ULLONG;
typedef unsigned char							EP_UCHAR;
typedef short									EP_SHORT;
typedef unsigned short							EP_USHORT;
typedef unsigned long							EP_ULONG;
typedef long									EP_LONG;



#define EP_VOID                               void

#define ARRAY_SIZE(x) ((unsigned)(sizeof(x) / sizeof((x)[0])))

#define EP_MEMSET(ARG) do {\
	memset(ARG, 0, sizeof(*ARG));}while(0)

#define EP_MEMSET_2(ARG, LEN) do {\
	memset(ARG, 0, LEN);}while(0)


#define CHECK_NULL(PTR, LABEL) do {\
	if (PTR == EP_NULL) goto failed_##LABEL; }while(0)


#define CHECK_FAILED(RET, LABEL) do {\
	if (RET == RET_FAILURE) goto failed_##LABEL;}while(0)

#define CHECK_SUCCESS(RET, VALUE, LABEL) do {\
	if (RET != VALUE) goto failed_##LABEL;}while(0)

#define EP_MALLOC(PTR) do {\
	PTR = calloc(1, sizeof (*PTR)); \
	assert(PTR);\
	}while(0)

#define EP_MALLOC_WITH_SIZE(PTR, SIZE) do {\
	PTR=calloc (1, SIZE); \
	assert(PTR);  \
	}while(0)


#define EP_FREE(PTR) do {\
	if (PTR) {free(PTR); PTR=EP_NULL;}} while(0)

#define EP_ERR_PTR   EC_CHAR *errStr = NULL

#define EP_ERR_SET(ERRMSG) do { \
	if (!errStr) { errStr = ERRMSG;} \
	} while (0)

#define EP_ERR_OUT() do { \
	if(errStr) {perror(errStr);}}while(0)
#define CLOSE_FD(FD) do { \
	if (FD > 0) {close(FD);} FD = -1;}while(0)

#define EP_DUMP_NUM(VALUE)			fprintf(stderr,  #VALUE "-->%d\n", VALUE)
#define EP_DUMP_STR(VALUE)			fprintf(stderr,  #VALUE "-->%s\n", VALUE)


#define EP_FILE_NAME_MAX_LEN				(1024)

#define EC_START_THREAD(RET, PID, FUNC) do {\
	RET= pthread_create(PID, NULL,  FUNC, NULL);} while(0)


#define EC_JSON_GET_OBJ(NODE, ROOT, STR)  do { \
	NODE = cJSON_GetObjectItem(ROOT, STR); \
	if (!NODE) {return EC_FAILURE;} }while(0)

#define EC_JSON_GET_ARR(NODE, ROOT, INDEX) do{\
	NODE = cJSON_GetArrayItem(ROOT, INDEX); \
	if (!NODE) {return EC_FAILURE;} }while(0)

#define EC_GET_CONF_STR(KEY, VALUE) do {\
	if (KEY) {free (KEY); KEY=NULL;} \
	if (VALUE != NULL) {KEY=strdup (VALUE);}} while (0)

#define EC_GET_CONF_INT(KEY, VALUE) do {\
	KEY = VALUE;}while(0)

#define EP_JSON_GET_INT(ROOT, KEY, DATA) do {\
	cJSON *node = NULL; \
	EP_JSON_GET_OBJ(node, ROOT, KEY); \
	EP_GET_CONF_INT(DATA, node->valueint);}while (0)

#define EP_JSON_GET_STR(ROOT, KEY, DATA) do {\
	cJSON *node = NULL; \
	EP_JSON_GET_OBJ(node, ROOT, KEY); \
	EP_GET_CONF_STR(DATA, node->valuestring);}while(0)

#define EP_JSON_SET_INT(ROOT, KEY, VALUE) do {\
	cJSON_AddNumberToObject(ROOT, KEY, VALUE);}while(0)

#define EP_JSON_SET_STR(ROOT, KEY, VALUE) do {\
	if (VALUE) {cJSON_AddStringToObject(ROOT, KEY, VALUE);} \
	else{cJSON_AddNullToObject(ROOT, KEY);}}while(0)

#define EP_JSON_SET_BOOL(ROOT, KEY, VALUE) do {\
	cJSON_AddBoolToObject(ROOT, KEY, VALUE);}while(0)

#define EP_WRITE_LOG(PATH, VALUE) do {\
	time_t now;\
    struct tm *local;\
	char cmd_str[512];\
	time(&now);\
    local = localtime(&now);\
	sprintf(cmd_str, "echo '%d-%02d-%02d %02d:%02d:%02d: %s' >> %s", local->tm_year + 1900, local->tm_mon + 1, local->tm_mday, local->tm_hour, local->tm_min, local->tm_sec, VALUE, PATH);\
	printf("logstr: %s\n", cmd_str);\
	system(cmd_str);}while (0)

#endif //ZKEP_ATP_THP_COMM_H
